package org.jeecg.modules.quartz.job;

import java.util.List;

import org.jeecg.modules.system.service.ISysDepartService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.finer.erp.jeecg.bas.entity.Fukuandan;
import io.finer.erp.jeecg.bas.entity.Lingliaodan;
import io.finer.erp.jeecg.bas.entity.Rukudan;
import io.finer.erp.jeecg.bas.entity.Shoukuandan;
import io.finer.erp.jeecg.bas.entity.Xiaoshouchuku;
import io.finer.erp.jeecg.bas.entity.Yingfudan;
import io.finer.erp.jeecg.bas.entity.Yingshoudan;
import io.finer.erp.jeecg.bas.service.IBasMaterialService;
import io.finer.erp.jeecg.bas.service.IFukuandanService;
import io.finer.erp.jeecg.bas.service.IKemuService;
import io.finer.erp.jeecg.bas.service.ILingliaodanService;
import io.finer.erp.jeecg.bas.service.IRukudanService;
import io.finer.erp.jeecg.bas.service.IShoukuandanService;
import io.finer.erp.jeecg.bas.service.IWeiwaijiagongService;
import io.finer.erp.jeecg.bas.service.IXiaoshouchukuService;
import io.finer.erp.jeecg.bas.service.IYingfudanService;
import io.finer.erp.jeecg.bas.service.IYingshoudanService;
import io.finer.erp.jeecg.finance.entity.FinPayable;
import io.finer.erp.jeecg.finance.entity.FinPayment;
import io.finer.erp.jeecg.finance.entity.FinReceipt;
import io.finer.erp.jeecg.finance.entity.FinReceivable;
import io.finer.erp.jeecg.finance.service.IFinPayableService;
import io.finer.erp.jeecg.finance.service.IFinPaymentService;
import io.finer.erp.jeecg.finance.service.IFinReceiptService;
import io.finer.erp.jeecg.finance.service.IFinReceivableService;
import io.finer.erp.jeecg.stock.entity.StkIoBill;
import io.finer.erp.jeecg.stock.service.IStkIoBillService;
/**
 * 
* @ClassName: CallBackKindeeIdJob 
* @Description: 定时扫描中间表数据取回金蝶凭证编号 
* @author heckerstone heckerstone@163.com
* @date Nov 2, 2020 3:59:01 PM 
*
 */
public class CallBackKindeeIdJob implements Job {
	
	/**
	 * 注入service
	 */
	@Autowired
	private IShoukuandanService shoukuandanService;
	@Autowired
	private IYingshoudanService yingshoudanService;
	@Autowired
	private IFukuandanService fukuandanService;
	@Autowired
	private IYingfudanService yingfudanService;
	@Autowired
	private IKemuService kemuService;
    @Autowired
	private ILingliaodanService lingliaodanService;
	@Autowired
	private IRukudanService rukudanService;
	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private IWeiwaijiagongService weiwaijiagongService;
	@Autowired
	private IBasMaterialService basMaterialService;
	@Autowired
	private IXiaoshouchukuService xiaoshouchukuService;
	
	@Autowired
	private IFinReceivableService finReceivableService;
	@Autowired
	private IFinReceiptService finReceiptService;
	@Autowired
	private IFinPaymentService finPaymentService;
	@Autowired
	private IFinPayableService finPayableService;
	@Autowired
	private IStkIoBillService stkIoBillService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		//收款单取回凭证编号
		getKindeeIDBySKD();
		
		//付款
		getKindessIdByFKD();
		
		//应付
		getKindeeIdByYFD();
		
		//应收
		getKindeeIdByYSD();
		
		//入库
		getKindeeIdByRuku();
		
		//销售出库
		getKindeeIdByXSCK();
		
	}
	
	/**
	 * 出库
	 */
	public void getKindeeIdByXSCK() {
		QueryWrapper<Xiaoshouchuku> queryWrapper=new QueryWrapper<Xiaoshouchuku>();
		queryWrapper.eq("is_upload_kindee_id", "0");
		queryWrapper.and(wrapper->wrapper.isNotNull("kindee_id"));
		List<Xiaoshouchuku> skList=xiaoshouchukuService.list(queryWrapper);
		if(null!=skList&&skList.size()>0) {
			for(Xiaoshouchuku sk:skList) {
				if(sk.getBillType().equals("销售出库单")||sk.getBillType().equals("销售退货")) {
					//取回凭证编号
					QueryWrapper<StkIoBill> fpQw=new QueryWrapper<StkIoBill>();
					fpQw.eq("bill_no", sk.getBillNo());
					StkIoBill fp=stkIoBillService.getOne(fpQw.last("LIMIT 1"));
					if(null!=fp) {
						fp.setKindeeId(sk.getKindeeId());
						stkIoBillService.updateById(fp);
					}
				}
				//将该条数据置为已取回
				sk.setIsUploadKindeeId(1);
				xiaoshouchukuService.updateById(sk);
			}
		}
	}
	
	/**
	 * 入库单
	 */
	public void getKindeeIdByRuku() {
		QueryWrapper<Rukudan> queryWrapper=new QueryWrapper<Rukudan>();
		queryWrapper.eq("is_upload_kindee_id", "0");
		queryWrapper.and(wrapper->wrapper.isNotNull("kindee_id"));
		List<Rukudan> skList=rukudanService.list(queryWrapper);
		if(null!=skList&&skList.size()>0) {
			for(Rukudan sk:skList) {
				if(sk.getBillType().equals("切张--产品入库")||sk.getBillType().equals("分切--产品入库")||sk.getBillType().equals("复合--产品入库")||sk.getBillType().equals("采购单入库")||sk.getBillType().equals("采购退货")) {
					//取回凭证编号
					QueryWrapper<StkIoBill> fpQw=new QueryWrapper<StkIoBill>();
					fpQw.eq("bill_no", sk.getBillNo());
					StkIoBill fp=stkIoBillService.getOne(fpQw.last("LIMIT 1"));
					if(null!=fp) {
						fp.setKindeeId(sk.getKindeeId());
						stkIoBillService.updateById(fp);
					}
				}
				//将该条数据置为已取回
				sk.setIsUploadKindeeId(1);
				rukudanService.updateById(sk);
			}
		}
	}
	
	/**
	 * 领料
	 */
	public void getKindeeIdByLingliao() {
		QueryWrapper<Lingliaodan> queryWrapper=new QueryWrapper<Lingliaodan>();
		queryWrapper.eq("is_upload_kindee_id", "0");
		queryWrapper.and(wrapper->wrapper.isNotNull("kindee_id"));
		List<Lingliaodan> skList=lingliaodanService.list(queryWrapper);
		if(null!=skList&&skList.size()>0) {
			for(Lingliaodan sk:skList) {
				//取回凭证编号
				QueryWrapper<StkIoBill> fpQw=new QueryWrapper<StkIoBill>();
				fpQw.eq("bill_no", sk.getBillNo());
				StkIoBill fp=stkIoBillService.getOne(fpQw.last("LIMIT 1"));
				if(null!=fp) {
					fp.setKindeeId(sk.getKindeeId());
					stkIoBillService.updateById(fp);
				}
				//将该条数据置为已取回
				sk.setIsUploadKindeeId(1);
				lingliaodanService.updateById(sk);
			}
		}
	}
	
	/**
	 * 收款单数据取回凭证ID
	 * 
	 */
	public void getKindeeIDBySKD() {
		QueryWrapper<Shoukuandan> queryWrapper=new QueryWrapper<Shoukuandan>();
		queryWrapper.eq("is_upload_kindee_id", "0");
		queryWrapper.and(wrapper->wrapper.isNotNull("kindee_id"));
		List<Shoukuandan> skList=shoukuandanService.list(queryWrapper);
		if(null!=skList&&skList.size()>0) {
			for(Shoukuandan sk:skList) {
				//取回凭证编号
				if(sk.getBillType().equals("销售预收款单")||sk.getBillType().equals("销售收款单")) {
					QueryWrapper<FinReceipt> fpQw=new QueryWrapper<FinReceipt>();
					fpQw.eq("bill_no", sk.getBillNo());
					FinReceipt fp=finReceiptService.getOne(fpQw.last("LIMIT 1"));
					if(null!=fp) {
						fp.setKindeeId(sk.getKindeeId());
						finReceiptService.updateById(fp);
					}
				}
				//将该条数据置为已取回
				sk.setIsUploadKindeeId(1);
				shoukuandanService.updateById(sk);
			}
		}
	}
	
	/**
	 * 付款单
	 */
	public void getKindessIdByFKD() {
		QueryWrapper<Fukuandan> queryWrapper=new QueryWrapper<Fukuandan>();
		queryWrapper.eq("is_upload_kindee_id", "0");
		queryWrapper.and(wrapper->wrapper.isNotNull("kindee_id"));
		List<Fukuandan> ysList=fukuandanService.list(queryWrapper);
		if(null!=ysList&&ysList.size()>0) {
			for(Fukuandan ys:ysList) {
				if(ys.getBillType().equals("采购预付单")||ys.getBillType().equals("采购付款单")) {
					QueryWrapper<FinPayment> fpQw=new QueryWrapper<FinPayment>();
					fpQw.eq("bill_no", ys.getBillNo());
					FinPayment fp=finPaymentService.getOne(fpQw.last("LIMIT 1"));
					if(null!=fp) {
						fp.setKindeeId(ys.getKindeeId());
						finPaymentService.updateById(fp);
					}
				}
				ys.setIsUploadKindeeId(1);
				fukuandanService.updateById(ys);
			}
		}
	}
	
	/**
	 * 应付
	 * 
	 */
	public void getKindeeIdByYFD() {
		QueryWrapper<Yingfudan> queryWrapper=new QueryWrapper<Yingfudan>();
		queryWrapper.eq("is_upload_kindee_id", "0");
		queryWrapper.and(wrapper->wrapper.isNotNull("kindee_id"));
		List<Yingfudan> ysList=yingfudanService.list(queryWrapper);
		if(null!=ysList&&ysList.size()>0) {
			for(Yingfudan ys:ysList) {
				if(ys.getBillType().equals("采购应付单")) {
					QueryWrapper<FinPayable> fpQw=new QueryWrapper<FinPayable>();
					fpQw.eq("bill_no", ys.getBillNo());
					FinPayable fp=finPayableService.getOne(fpQw.last("LIMIT 1"));
					if(null!=fp) {
						fp.setKindeeId(ys.getKindeeId());
						finPayableService.updateById(fp);
					}
				}
				ys.setIsUploadKindeeId(1);
				yingfudanService.updateById(ys);
			}
		}
	}
	
	/**
	 * 应收
	 */
	public void getKindeeIdByYSD() {
		QueryWrapper<Yingshoudan> queryWrapper=new QueryWrapper<Yingshoudan>();
		queryWrapper.eq("is_upload_kindee_id", "0");
		queryWrapper.and(wrapper->wrapper.isNotNull("kindee_id"));
		List<Yingshoudan> ysList=yingshoudanService.list(queryWrapper);
		if(null!=ysList&&ysList.size()>0) {
			for(Yingshoudan ys:ysList) {
				if(ys.getBillType().equals("销售应收单")) {
					QueryWrapper<FinReceivable> fpQw=new QueryWrapper<FinReceivable>();
					fpQw.eq("bill_no", ys.getBillNo());
					FinReceivable fp=finReceivableService.getOne(fpQw.last("LIMIT 1"));
					if(null!=fp) {
						fp.setKindeeId(ys.getKindeeId());
						finReceivableService.updateById(fp);
					}
				}
				ys.setIsUploadKindeeId(1);
				yingshoudanService.updateById(ys);
			}
		}
	}

}
